#!/bin/sh

DAS_BIN=$1
CONFIG_FILE=$2
OUTPUT_FILE=$3

if [[ "${DAS_BIN}" == "" ]] ; then
    DAS_BIN=/data/das/bin/das
fi

if [[ "${CONFIG_FILE}" == "" ]] ; then
    CONFIG_FILE=/data/das/config/das.yaml

    if [[ "${MY_CONFIG_FILE}" != "" ]] ; then
        CONFIG_FILE=${MY_CONFIG_FILE}
    fi
fi

if [[ "${OUTPUT_FILE}" == "" ]] && [[ "${MY_POD_NAME}" != "" ]] ; then
    OUTPUT_FILE=/data/das/log/${MY_POD_NAME}-output.log.crn
fi

if [[ "${OUTPUT_FILE}" != "" ]] && [[ -f "${OUTPUT_FILE}" ]] ; then
        now=`date +%Y%m%d%H%M%S`
        base=`echo ${OUTPUT_FILE} | awk -F'.log.crn' '{print $1}'`
        mv ${OUTPUT_FILE} ${base}-${now}.log
fi

DAS_COMMAND="${DAS_BIN} start --config=${CONFIG_FILE}"

if [[ "${OUTPUT_FILE}" != "" ]]; then
    ${DAS_COMMAND} 2>&1 | tee -a ${OUTPUT_FILE}
else
    ${DAS_COMMAND}
fi